package com.smart.reader.controller;

import com.smart.reader.common.Page;
import com.smart.reader.common.R;
import com.smart.reader.enums.SuccessResultCode;
import com.smart.reader.exception.CustomException;
import com.smart.reader.model.bo.SysUserInfoAddBo;
import com.smart.reader.model.bo.SysUserInfoUpdateBo;
import com.smart.reader.model.vo.SysUserInfoVo;
import com.smart.reader.service.SysUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@RequestMapping("/api/v1/system/userInfo")
@RestController
@Validated
@Api(tags = "⽤户管理", description = "⽤户管理相关接⼝")
public class SysUserInfoController {
    @Resource
    private SysUserInfoService sysUserInfoService;
    /**
     * 新增数据
     *
     * @param sysUserInfo 实例对象
     * @return 实例对象
     */
    @ApiOperation("新增数据")
    @PostMapping("/add")
    public R add(@RequestBody @Valid SysUserInfoAddBo sysUserInfo) throws CustomException {
        sysUserInfoService.add(sysUserInfo);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg());
    }
    @ApiOperation("删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "⽤户ID", required = true)
    })
    @DeleteMapping("/delete/{id}")
    public R delete(@NotNull(message = "⽤户ID不能为空") @PathVariable("id") Long id) throws CustomException {
        System.out.println("删除的用户ID: " + id);
        sysUserInfoService.delete(id);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg());
    }
    @ApiOperation("修改数据")
    @PostMapping("/edit")
    public R edit(@RequestBody @Valid SysUserInfoUpdateBo sysUserInfo) throws CustomException {
        sysUserInfoService.edit(sysUserInfo);
        return R.fail(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg());
    }
    @GetMapping("queryInfo")
    @ApiOperation("获取⽤户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前⻚", required
                    = true, dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "⻚⼤⼩", required = true, dataType = "int", defaultValue = "10"),
    })
    public R<Page<SysUserInfoVo>> queryInfo(int pageNum, int pageSize) {
        Page<SysUserInfoVo> result = sysUserInfoService.queryInfo(pageNum, pageSize);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg(), result);
    }
    @ApiOperation("根据⽤户ID查询⽤户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "⽤户ID", required = true)
    })
    @GetMapping("/detail/{id}")
    public R detail(@NotNull(message = "⽤户ID不能为空") @PathVariable("id") Long id) {
        SysUserInfoVo sysUserInfoVo = sysUserInfoService.detail(id);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg(), sysUserInfoVo);
    }
}
